import {defineStore} from 'pinia';

const statusOption = [
    {label: "正常", value: "0"},
    {label: "停用", value: "1"}
]

const API = {
    LIST: '/menu/tree',
    ADD: "/menu",
    MODIFY: "/menu",
    INFO: "/menu/details",
    REMOVE: "/menu"
}

const COLUMNS = [
    {field: "name", title: "菜单名称", align: "left"},
    {field: "icon", title: "图标", align: "center", width: "150px"},
    {field: "permission", title: "权限标识", align: "center", width: "250px"},
    {field: "componentPath", title: "组件路径", align: "center", width: "250px"},
    // {field: "status", title: "状态", align: "center", width: "150px"},
    {field: "visible", title: "是否可见", align: "center", width: "150px"},
    // {field: "createTime", title: "创建时间", align: "center", width: "220px"},
]

const SEARCH_FORM = [
    {field: "name", title: "菜单名称", placeholder: "请输入菜单名称"},
    // {field: "status", title: "状态", options: statusOption},
]

const OPTIONS = [
    {emit: "modify", title: "修改"},
    {emit: "remove", title: "删除"},
]

const store = {
    id: "sysMenu",
    state(){
        return {
            table: null,
            pageNo: 1,
            pageSize: 10,
            searchForm: {},
            dataList: []
        }
    },

    getters: {

    },

    actions: {
        init(table) {
            this.table = table;
            this.table.setColumns(COLUMNS);
            this.table.setSearchForm(SEARCH_FORM);
            this.table.setOptions(OPTIONS);
        },

        refresh(isFirstPage) {
            if (isFirstPage) {
                this.getList(1, 10)
                return
            }
            this.getList(this.pageNo, this.pageSize)
        },

        /**
         * 列表数据
         * @param pageNo
         * @param pageSize
         * @param condition
         * @returns {Promise<void>}
         */
        async getList(condition) {
            let params = {};
            for (let key in condition) {
                if(! Helper.empty(condition[key])) {
                    params[key] = condition[key];
                }
            }

            Msg.loading("加载中...")
            let {data} = await Http.get(API.LIST, params);
            Msg.loading(false)

            let dataList = []
            if(! Helper.emptyArray(data)) {
                dataList = data
            }
            this.dataList = dataList
            this.table.setData(dataList);
        },

        /**
         * 新增
         * @param params
         * @returns {Promise<undefined>}
         */
        async add(params) {
            Msg.loading("正在提交...")
            let {code, data} = await Http.post(API.ADD, params);
            Msg.loading(false);

            if(data) {
                Msg.success("保存成功");
                this.refresh(true);
                return true;
            } else {
                Msg.error("保存失败, 请重新尝试");
                return false;
            }
        },

        /**
         * 获取详情
         * @param id
         * @returns {Promise<undefined|boolean>}
         */
        async getInfo(id) {
            Msg.loading("加载中...");
            let {code, data} = await Http.get(API.INFO, {menuId: id})
            Msg.loading(false);

            if (! Helper.emptyJson(data)) {
                return data
            }
            return false
        },

        /**
         * 修改
         * @param params
         * @returns {Promise<undefined>}
         */
        async modify(params) {
            Msg.loading("正在提交...")
            let {data} = await Http.put(API.MODIFY, params);
            Msg.loading(false);

            if(data) {
                Msg.success("保存成功");
                this.refresh(true);
            } else {
                Msg.error("保存失败, 请重新尝试")
            }
            return data
        },

        /**
         * 删除
         * @param id
         * @returns {Promise<undefined>}
         */
        async remove(id) {
            Msg.loading("正在删除...");
            let {data} = await Http.delete(API.REMOVE + "/" + id)
            Msg.loading(false)

            if(data) {
                Msg.success("删除成功");
                this.refresh()
            } else {
                Msg.error("删除失败，请重新尝试");
            }
            return data
        },
    },
}

export default defineStore(store)
